package ch.bfh.mobicomp.soda2.gps_datacollector;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import ch.bfh.mobicomp.soda2.gps_datacollector.business.SODA_GPS_DataCollector_Service;
import ch.bfh.mobicomp.soda2.gps_datacollector.business.utils.SODA_GPS_SharedPreferenceManager;

public class SODA_GPS_BroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = SODA_GPS_BroadcastReceiver.class.getName();

    private String intentAction = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null) {
            intentAction = intent.getAction();
            Log.d(TAG, "IntentAction: " + intentAction);
            if (intentAction.equals(SODA_GPS_DataCollector_Service.GPS_ONSTART_INTENT)) {
                intent.setClass(context, SODA_GPS_DataCollector_Service.class);
                context.startService(intent);
                SODA_GPS_SharedPreferenceManager.getInstance(context).saveStateStarted();
                Log.d(TAG, "Service started");
            } else if (intentAction.equals(SODA_GPS_DataCollector_Service.GPS_ONSTOP_INTENT)) {
                intent.setClass(context, SODA_GPS_DataCollector_Service.class);
                context.stopService(intent);
                SODA_GPS_SharedPreferenceManager.getInstance(context).saveStateStoped();
                Log.d(TAG, "Service stoped");
            } else if (intentAction.equals("android.intent.action.BOOT_COMPLETED")) {
                Log.d(TAG, "Device Booted");
                
                float distDelay = SODA_GPS_SharedPreferenceManager.getInstance(context).getMinDistanceDelay();
                long timeDelay = SODA_GPS_SharedPreferenceManager.getInstance(context).getMinTimeDelay();
                intent = SODA_GPS_DataCollector_Service.getStartGPSServicetIntent(distDelay, timeDelay);
                intent.setClass(context, SODA_GPS_DataCollector_Service.class);
                context.startService(intent);
                
                Log.d(TAG, "Service Started");
            } else {
                Log.e(TAG, "No Action found");
            }
        } else {
            Log.e(TAG, "Action was Null");
        }

    }
}
